package br.org.mestrado.vais.comparator;

import java.util.Comparator;

import br.org.mestrado.vais.Anticorpo;

public abstract class ParetoCompare implements Comparator<Anticorpo> {

	protected final int numObjetivos;

	protected double S;

	public abstract int compare(Anticorpo arg0, Anticorpo arg1);

	public ParetoCompare(final int totalObjectives) {
		numObjetivos = totalObjectives;
	}

	public void setS(final double s) {
		S = s;
	}

	protected double[] modifyPareto(final double... objectivesValues) {

		final double[] copy = new double[objectivesValues.length];

		final double r = r(objectivesValues);

		for (int pointer = 0; pointer < objectivesValues.length; pointer++) {

			double cosWi = objectivesValues[pointer] / r;

			double cosWi2 = cosWi * cosWi;

			double senWi = Math.sqrt(1 - cosWi2);

			double senSiPi = Math.sin(S * Math.PI);

			double cosSiPi = Math.cos(S * Math.PI);

			// Formula: r*sen(Wi+SiPi)/sen(SiPi)
			double numerador = r * ((senWi * cosSiPi) + (cosWi * senSiPi));

			double novoFix = numerador / senSiPi;

			copy[pointer] = Math.max(novoFix, 0);

		}

		return copy;

	}

	public double r(double[] objetivos) {

		double soma = 0;

		for (double fix : objetivos) {

			soma += Math.pow(fix, 2);
		}

		return Math.sqrt(soma);
	}

}